/*
 * Author : Abdelghani Bhalla
 * This class has CRUD functions for parent.model
 * 
 */
package edu.bu.cs673.superkids123.parents.DataObjectAccess;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import edu.bu.cs673.superkids123.HibernateUtil;
import edu.bu.cs673.superkids123.common.model.Achievement;
import edu.bu.cs673.superkids123.parents.model.Color;
import edu.bu.cs673.superkids123.parents.model.Image;
import edu.bu.cs673.superkids123.parents.model.Sound;
import edu.bu.cs673.superkids123.parents.model.Number;
import edu.bu.cs673.superkids123.parents.model.SystemTheme;

public class ParentSystemManager {

	// Add Color
	public static int addColor(String name, String code) {
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Color color = new Color(name, code);

			result = (Integer) session.save(color);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get Color By Id
	public static Color getColorById(int id) {
		Color color = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Color.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			color = (Color) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return color;
	}

	// Get Color By code
	public static Color getColorByCode(String code) {
		Color color = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Color.class);
			criteria.add(Restrictions.eq("code", code)).setMaxResults(1);

			color = (Color) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return color;
	}

	// Get All Colors
	@SuppressWarnings("unchecked")
	public static List<Color> getColors() {
		List<Color> colors = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Query query = session.createQuery("From Color");

			colors = (List<Color>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return colors;
	}

	// Add Image
	public static int addImage(String name, String fileName) {
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Image image = new Image(name, fileName);

			result = (Integer) session.save(image);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get Image By Id
	public static Image getImageById(int id) {
		Image image = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Image.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			image = (Image) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return image;
	}

	// Get Image By fileName
	public static Image getImageByFileName(String fileName) {
		Image image = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Image.class);
			criteria.add(Restrictions.eq("fileName", fileName))
					.setMaxResults(1);

			image = (Image) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return image;
	}

	// Get All Images
	@SuppressWarnings("unchecked")
	public static List<Image> getImages() {
		List<Image> images = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Query query = session.createQuery("From Image");

			images = (List<Image>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return images;
	}

	// Add Sound
	public static int addSound(String name, String fileName) {
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Sound sound = new Sound(name, fileName);

			result = (Integer) session.save(sound);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get Sound By Id
	public static Sound getSoundById(int id) {
		Sound sound = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Sound.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			sound = (Sound) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return sound;
	}

	// Get Sound By fileName
	public static Sound getSoundByFileName(String fileName) {
		Sound sound = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Sound.class);
			criteria.add(Restrictions.eq("fileName", fileName))
					.setMaxResults(1);

			sound = (Sound) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return sound;
	}

	// Get All Sound
	@SuppressWarnings("unchecked")
	public static List<Sound> getSounds() {
		List<Sound> sounds = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Query query = session.createQuery("From Sound");

			sounds = (List<Sound>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return sounds;
	}

	// Add Number
	public static int addNumber(int number, Image image, Sound sound) {
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			Number num = new Number(number);
			image.addNumber(num);
			sound.addNumber(num);
			result = (Integer) session.save(num);

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get All Number
	@SuppressWarnings("unchecked")
	public static List<Number> getNumbers() {
		List<Number> numbers = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Query query = session
					.createQuery("From Number order by number asc");

			numbers = (List<Number>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return numbers;
	}

	// Get Number by Id
	public static Number getNumberById(int id) {
		Number number = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Number.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			number = (Number) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return number;
	}

	// Get Number by number Digit
	public static Number getNumberByNumberDigit(int numberDigit) {
		Number number = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(Number.class);
			criteria.add(Restrictions.eq("number", numberDigit)).setMaxResults(
					1);

			number = (Number) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return number;
	}

	// Themes
	// Add Theme
	public static int addTheme(String name, String font, String description,
			Color color, Sound sound) {
		int result = -1;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			SystemTheme theme = new SystemTheme(name, font, description);
			color.addTheme(theme);
			sound.addTheme(theme);
			result = (Integer) session.save(theme);

			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return result;
	}

	// Get All Themes
	@SuppressWarnings("unchecked")
	public static List<SystemTheme> getSystemThemes() {
		List<SystemTheme> systemThemes = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Query query = session
					.createQuery("From SystemTheme order by name asc");

			systemThemes = (List<SystemTheme>) query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return systemThemes;
	}

	// Get System Theme by Id
	public static SystemTheme getSystemThemeById(int id) {
		SystemTheme theme = null;
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();

			Criteria criteria = session.createCriteria(SystemTheme.class);
			criteria.add(Restrictions.eq("id", id)).setMaxResults(1);

			theme = (SystemTheme) criteria.uniqueResult();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return theme;
	}
}
